package ihis.core.db;

import ihis.model.GeneralFilter;
import ihis.model.IHISConstants;
import ihis.model.Performance;
import ihis.model.PredictedCurve;
import ihis.model.Schedule;
import ihis.model.Task;
import ihis.model.TaskFilter;

import java.io.Reader;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class IHISDatabase {
	private static SqlMapClient sqlMap;
	
	public synchronized static SqlMapClient getSqlMapClient()
	{
		if(sqlMap == null){
			try{
				String resource = "SqlMapConfig.xml";
				Reader reader = Resources.getResourceAsReader(resource);
				sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
				reader.close();
			}catch(Exception e){
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		
		return sqlMap;
	}
	
	/**
	 * 한 건의 Schedule 정보를 DB에 입력한다.
	 * @param schedule
	 * @return
	 */
	public synchronized static int insertSchedule(Schedule schedule) {
		int result = IHISConstants.FAIL;
		
		try {
			TaskFilter filter = new TaskFilter();
			filter.setOwnerId(schedule.getOwnerId());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			filter.setReportDate(sdf.format(schedule.getReportDate()));
			filter.setOwnerType(schedule.getOwnerType());
			filter.setPeriodType(schedule.getPeriodType());
			filter.setProjectId(schedule.getProjectId());
			
			Schedule task = (Schedule) IHISDatabase.getSqlMapClient().queryForObject("SelectOne.ScheduleByFilter", filter);
			if(task == null)
				IHISDatabase.getSqlMapClient().insert("Insert.Schedule", schedule);
			else
				IHISDatabase.getSqlMapClient().update("Update.Schedule", schedule);
			result = IHISConstants.SUCCESS;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * @param filter
	 * @return
	 */
	public synchronized static int deleteSchedule(GeneralFilter filter) {
		int result = IHISConstants.FAIL;
		
		try {
			result = (Integer) IHISDatabase.getSqlMapClient().delete("Delete.Schedule", filter);
			if(result == 1){
				result = IHISConstants.SUCCESS;
			} else {
				result = IHISConstants.FAIL;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	/**
	 * @param curve
	 */
	public synchronized static int insertPredictedCurve(PredictedCurve curve) {
		int result = IHISConstants.FAIL;
		
		try {
			TaskFilter filter = new TaskFilter();
			filter.setOwnerId(curve.getOwnerId());
			filter.setTeamId(curve.getTeamId());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			filter.setReportDate(sdf.format(curve.getMeasureDate()));
			filter.setOwnerType(curve.getOwnerType());
			filter.setPeriodType(curve.getPeriodType());
			filter.setProjectId(curve.getProjectId());
			
			PredictedCurve task = (PredictedCurve) IHISDatabase.getSqlMapClient().queryForObject("SelectOne.PredictedCurveByFilter", filter);
			if(task == null)
				IHISDatabase.getSqlMapClient().insert("Insert.PredictedCurve", curve);
			else
				IHISDatabase.getSqlMapClient().update("Update.PredictedCurve", curve);
			result = IHISConstants.SUCCESS;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * @param filter
	 * @return
	 */
	public synchronized static int deletePredictedCurve(GeneralFilter filter) {
		int result = IHISConstants.FAIL;
		
		try {
			result = (Integer) IHISDatabase.getSqlMapClient().delete("Delete.PredictedCurve", filter);
			if(result == 1){
				result = IHISConstants.SUCCESS;
			} else {
				result = IHISConstants.FAIL;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * @param risk
	 */
	public synchronized static int insertRisk(Task risk) {
		int result = IHISConstants.FAIL;
		
		try {
			Task isExist = (Task)IHISDatabase.getSqlMapClient().insert("SelectOne.Task.Risk", risk.getTaskId());
			if(isExist == null)
			{
				IHISDatabase.getSqlMapClient().insert("Insert.Task", risk);
			}
			result = IHISConstants.SUCCESS;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * @param filter
	 */
	public synchronized static int deleteRisk(TaskFilter filter) {
		int result = IHISConstants.FAIL;
		
		try {
			result = (Integer) IHISDatabase.getSqlMapClient().delete("Delete.Task", filter);
			if(result == 1){
				result = IHISConstants.SUCCESS;
			} else {
				result = IHISConstants.FAIL;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * @param performance
	 */
	public static int insertPerformance(Performance performance) {
		int result = IHISConstants.FAIL;
		
		try {
			TaskFilter filter = new TaskFilter();
			filter.setOwnerId(performance.getOwnerId());
			filter.setTeamId(performance.getTeamId());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			filter.setReportDate(sdf.format(performance.getReportDate()));
			filter.setPeriodType(performance.getPeriodType());
			filter.setProjectId(performance.getProjectId());
			
			Performance task = (Performance) IHISDatabase.getSqlMapClient().queryForObject("SelectOne.PerformanceByFilter", filter);
			if(task == null)
				IHISDatabase.getSqlMapClient().insert("Insert.Performance", performance);
			else
				IHISDatabase.getSqlMapClient().update("Update.Performance", performance);
			result = IHISConstants.SUCCESS;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * @param filter
	 */
	public static int deletePerformance(TaskFilter filter) {
		int result = IHISConstants.FAIL;
		
		try {
			result = (Integer) IHISDatabase.getSqlMapClient().delete("Delete.Performance", filter);
			if(result == 1){
				result = IHISConstants.SUCCESS;
			} else {
				result = IHISConstants.FAIL;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
}
